﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Log4Nez.Interfaces;
//using Log4Nez.Enumerations;
using Log4Nez.Exceptions;
using Log4Nez.Resources;
using Log4Nez.Factories;
using System.Diagnostics;

namespace Log4Nez.Loggers
{
    public static class Logger
    {
        //The doctionary of loggers identified by the name inside the configuration file
        private static Dictionary<string, ILogger> loggers = null;
        //private static TraceLevel traceLevel = TraceLevel.Error;

        #region Private Methods
        private static void loadSettings()
        {
            try
            {
                XmlDocument settings = new XmlDocument();
                System.Collections.Specialized.NameValueCollection colSet
                    = System.Configuration.ConfigurationManager.GetSection("OpenSourceFactory.Logger/ConfigFiles") as System.Collections.Specialized.NameValueCollection;
                string path = colSet["ConfigPath"];
                settings.Load(path);

                XmlNode logLevelNode = settings.SelectSingleNode("Log4Nez/Configuration/LogTraceLevel");
                if (logLevelNode != null)
                    Enum.TryParse<TraceLevel>(logLevelNode.Attributes["level"].Value, out traceLevel);
                else
                    throw new SettingsException(ExceptionMessages.LogLevelRequired);

                XmlNodeList logNodeList = settings.SelectSingleNode("Log4Nez/Loggers").ChildNodes;
                if (logNodeList == null)
                    throw new SettingsException(ExceptionMessages.AtLeastOneLoggerRequired);
                if (loggers == null)
                {
                    loggers = new Dictionary<string, ILogger>();
                    foreach (XmlNode item in logNodeList)
                        loggers.Add(item.Attributes["loggerName"].Value, LoggerFactoriesProxy.CreateLogger(item));
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        private static void writeLog(object message, string logName, TraceLevel traceLevel, params string[] formatParams)
        {
            if (loggers == null)
                loadSettings();
            if (loggers.ContainsKey(logName))
                loggers[logName].Write(traceLevel, message, formatParams);
            else
                Debugger.Log(0, "Log4Nez", string.Format(ExceptionMessages.UnableToFindLoggerNameInConfiguredLoggersStringFormat, logName));
        }
        #endregion

        public static void Information(object message, string logName, params string[] formatParams)
        {
            writeLog(message, logName, System.Diagnostics.TraceLevel.Info);
        }

        public static void Debug(object message, string logName, params string[] formatParams)
        {
            writeLog(message, logName, System.Diagnostics.TraceLevel.Info);
        }

        public static void Error(object message, string logName, params string[] formatParams)
        {
            writeLog(message, logName, System.Diagnostics.TraceLevel.Error, formatParams);
        }

        public static void Warning(object message, string logName, params string[] formatParams)
        {
            writeLog(message, logName, System.Diagnostics.TraceLevel.Warning);

        }
    }
}
